importScripts("../src/webrpc.js");

var bookIdSeq = 1;
var database = {};

new webrpc.Service("bookService", {
    getAll: function() {
	var results = [];
	for (var id in database) {
	    results.push(database[id]);
	}
	return results;
    },
    get: function(bookId) {
	return database[bookId];
    },
    save: function(book) {
	heavyLoop();
	if (!book.id) {
	    book.id = bookIdSeq++;
	}
	database[book.id] = book;
	return book;
    },
    remove: function(bookId) {
	heavyLoop();
	delete database[bookId];
    }
});

    function heavyLoop() {
	// CPU centric job, but browser is not freezed.
	for (var i = 0; i < 50000000; i++) {
	}
    }